package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.net.ssl.KeyManagerFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import server.Commands;

public class GUI implements ActionListener{
	private String keystore;
	private String password;
	private Connector connector;
	private final String path = "Certificates/";
	
	/**
	 * @param args
	 */
	public GUI(Connector connector){
		this.connector = connector;
		
		login();
		connector.connect();
		JFrame f = new JFrame("this be good stuffs");
		JPanel p = new JPanel();
		
		JButton load = new JButton("Load");
		JButton create = new JButton("Create");
		JButton delete = new JButton("Delete");
		JButton exit = new JButton("Exit");
		
		load.setActionCommand("load");
		create.setActionCommand("create");
		delete.setActionCommand("delete");
		exit.setActionCommand("exit");
		
		load.addActionListener(this);
		create.addActionListener(this);
		delete.addActionListener(this);
		exit.addActionListener(this);
		
		p.add(load);
		p.add(create);
		p.add(delete);
		p.add(exit);
		
		f.getContentPane().add(p);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.pack();
		f.setVisible(true);
	}
	
	private void login() {
		keystore = JOptionPane.showInputDialog("Keystore:");
		if(keystore == null) {System.out.println("Login aborted"); System.exit(0);}
		else if(keystore.equals("")){ System.out.println("Invalid keystore"); System.exit(0);}
		
		JPasswordField jpf = new JPasswordField();
		int choice = JOptionPane.showConfirmDialog(null, jpf,
		  "Password:", JOptionPane.OK_CANCEL_OPTION);
		password = String.copyValueOf(jpf.getPassword());
		if(choice == 2 || choice == -1 || password.equals("")){ System.out.println("Invalid or no password"); System.exit(0);}
		
		KeyManagerFactory kmf = null;
		KeyStore ks = null;
		char[] passphrase = password.toCharArray();
		try {
			kmf = KeyManagerFactory.getInstance("SunX509");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		try {
			ks = KeyStore.getInstance("JKS");
		} catch (KeyStoreException e) {
			e.printStackTrace();
		}

		try {
			ks.load(new FileInputStream(path+keystore), passphrase);
		} catch (NoSuchAlgorithmException e) {
			System.out.println("Keystore not found or wrong password");
			System.exit(0);
			e.printStackTrace();
		} catch (CertificateException e) {
			System.out.println("Keystore not found or wrong password");
			System.exit(0);
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			System.out.println("Keystore not found or wrong password");
			System.exit(0);
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("Keystore not found or wrong password");
			System.exit(0);
			e.printStackTrace();
		}
		try {
			kmf.init(ks, passphrase);
		} catch (UnrecoverableKeyException e) {
			System.out.println("Keystore not found or wrong password");
			System.exit(0);
			e.printStackTrace();
		} catch (KeyStoreException e) {
			System.out.println("Keystore not found or wrong password");
			System.exit(0);
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			System.out.println("Keystore not found or wrong password");
			System.exit(0);
			e.printStackTrace();
		}
		
		
		System.setProperty("javax.net.ssl.trustStore", path + keystore);
		System.setProperty("javax.net.ssl.trustStorePassword", password);
		System.setProperty("javax.net.ssl.keyStore", path + keystore);
		System.setProperty("javax.net.ssl.keyStorePassword", password);
	}

	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand().equals("load")){
			connector.setAction(Commands.LOAD);
			String s = JOptionPane.showInputDialog("Write filename to load");
			connector.sendString(s);
		}else if(e.getActionCommand().equals("create")){
			String fileName = JOptionPane.showInputDialog("Filnamn.");
			connector.createNewViewer(fileName);
		}else if(e.getActionCommand().equals("delete")){
			connector.setAction(Commands.DELETE);
			String s = JOptionPane.showInputDialog("Write filename to delete");
			connector.sendString(s);
		}else{
			connector.setAction(Commands.EXIT);
			System.exit(0);
		}
			
	}

}
